<!-- src/views/forum/create.vue -->
<template>
  <div class="post-create-container">
    <div class="header">
      <h2>发布新帖</h2>
    </div>

    <el-form
      :model="postForm"
      :rules="rules"
      ref="postForm"
      label-width="80px"
      class="post-form"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="postForm.title"
          placeholder="请输入帖子标题（5-50个字符）"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>

      <el-form-item label="内容" prop="content">
        <el-input
          type="textarea"
          v-model="postForm.content"
          placeholder="请输入帖子内容（10-2000个字符）"
          :rows="10"
          maxlength="2000"
          show-word-limit
        ></el-input>
      </el-form-item>

      <el-form-item class="form-buttons">
        <el-button @click="goBack">取消</el-button>
        <el-button type="primary" @click="submitForm" :loading="submitting"
          >发布帖子</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import forumApi from "@/api/forum";
import { mapState } from "vuex";

export default {
  name: "PostCreate",
  data() {
    return {
      postForm: {
        title: "",
        content: "",
        userId: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入帖子标题", trigger: "blur" },
          {
            min: 5,
            max: 50,
            message: "标题长度在5到50个字符之间",
            trigger: "blur",
          },
        ],
        content: [
          { required: true, message: "请输入帖子内容", trigger: "blur" },
          {
            min: 10,
            max: 2000,
            message: "内容长度在10到2000个字符之间",
            trigger: "blur",
          },
        ],
      },
      submitting: false,
    };
  },
  computed: {
    ...mapState("user", ["userInfo"]),
  },
  created() {
    this.checkUserProfile();
  },
  methods: {
    checkUserProfile() {
      if (!this.userInfo || !this.userInfo.id) {
        this.$message.error("请先登录");
        this.$router.push("/login");
        return;
      }
      this.postForm.userId = this.userInfo.id;
    },
    // 提交表单
    submitForm() {
      this.$refs.postForm.validate((valid) => {
        if (!valid) {
          return false;
        }

        this.submitting = true;
        forumApi
          .createPost(this.postForm)
          .then((res) => {
            if (res.code === 1) {
              this.$message.success("发布成功");
              this.$router.push("/forum/index");
            } else {
              this.$message.error(res.message || "发布失败");
            }
          })
          .catch((error) => {
            console.error("发布帖子失败:", error);
            this.$message.error("发布失败，请稍后重试");
          })
          .finally(() => {
            this.submitting = false;
          });
      });
    },
    // 返回论坛列表页
    goBack() {
      this.$router.push("/forum/index");
    },
  },
};
</script>

<style scoped>
.post-create-container {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  max-width: 800px;
  margin: 0 auto;
}

.header {
  margin-bottom: 30px;
  border-bottom: 1px solid #ebeef5;
  padding-bottom: 15px;
}

.post-form {
  margin-top: 20px;
}

.form-buttons {
  text-align: right;
  margin-top: 30px;
}
</style>
